package ${entity.getSiblingPackage("controller")};

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import ${entity.getSiblingPackage("dto")}.${entity.entityName}Dto;
import ${entity.getSiblingPackage("service")}.${entity.entityName}Service;
import wo.bsys.vo.WoDataTable;
import wo.bsys.vo.WoSelectorParams;
import wo.common.entity.WoPage;
import wo.common.exception.WoResultCode;
import wo.common.util.WoJsonUtil;
import wo.bsys.util.BSysConstant;

/**
 * ${entity.entityCN}控制器.
 * @author cailei
 */
@Controller
@RequestMapping("/${entity.module}/${entity.entityNameLowerFirstChar}")
@SessionAttributes(names = BSysConstant.SESSION_USER)
public class ${entity.entityName}Controller {

	/**
	 * 注入${entity.entityName}Service.
	 */
    @Resource // @Autowired
	private ${entity.entityName}Service ${entity.entityNameLowerFirstChar}Service;
	
	/**
	 * @param draw DataTable控件请求序列号,返回数据时需要设置该值
	 * @param start 当前页开始索引,从0开始
	 * @param length 当前页最大行数
	 * @param searchContent 查询内容
	 * @param dir 排序方式
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public WoDataTable<${entity.entityName}Dto> getDataTable(Integer draw, Long start, Long length,
			@RequestParam("search[value]") String searchContent, @RequestParam("order[0][dir]") String dir
            #if($entity.isWeak())
            ,${entity.weakRelationM21.target.firstKeyField.fieldType.name} ${entity.weakRelationM21.sourceFieldName}Id
            #end
			) {
		WoPage<${entity.entityName}Dto> page = ${entity.entityNameLowerFirstChar}Service.getPageData(start, length, searchContent, dir
		#if($entity.isWeak())
        ,${entity.weakRelationM21.sourceFieldName}Id
        #end
		);
		return new WoDataTable<${entity.entityName}Dto>(page, draw);
	}
	
	/**
	 * 加载"创建"页面
	 * @return
	 */
	@GetMapping("/create")
	public String create (Map<String, Object> map
		#if($entity.isWeak())
        ,${entity.weakRelationM21.target.firstKeyField.fieldType.name} ${entity.weakRelationM21.sourceFieldName}Id
        #end
		) {
		map.put("url", "${entity.entityNameLowerFirstChar}/create.jsp");
		
		#if($entity.isWeak())
		// 设置M21关联对象id
		${entity.entityName}Dto dto = new ${entity.entityName}Dto();
		dto.set${entity.weakRelationM21.sourceFieldNameUpperFirstChar}${entity.weakRelationM21.target.firstKeyField.fieldNameUpperFirstChar}(${entity.weakRelationM21.sourceFieldName}Id);
		map.put("formData", dto);
		#end
		return "main";
	}
	
	/**
	 * 提交"创建"表单
	 * @return
	 */
	@PostMapping("/create")
	public String create (${entity.entityName}Dto dto, Map<String, Object> map) {
		${entity.entityNameLowerFirstChar}Service.create(dto);
		#if($entity.isWeak())
		return "redirect:/${entity.weakRelationM21.target.module}/${entity.weakRelationM21.target.entityNameLowerFirstChar}/update?id=" + dto.get${entity.weakRelationM21.sourceFieldNameUpperFirstChar}${entity.weakRelationM21.target.firstKeyField.fieldNameUpperFirstChar}();
		#else
		return "redirect:/";
		#end
	}
	
	/**
	 * 加载"修改"页面
	 * @return
	 */
	@GetMapping("/update")
	public String update (String id, Map<String, Object> map) {
		${entity.entityName}Dto dto = ${entity.entityNameLowerFirstChar}Service.getById (id);
		map.put("url", "${entity.entityNameLowerFirstChar}/update.jsp");
		map.put("formData", dto);
		return "main";
	}
	
	/**
	 * 提交"修改"表单
	 * @return
	 */
	@PostMapping("/update")
	public String update (${entity.entityName}Dto dto, Map<String, Object> map) {
		${entity.entityNameLowerFirstChar}Service.update(dto);
		#if($entity.isWeak())
		return "redirect:/${entity.weakRelationM21.target.module}/${entity.weakRelationM21.target.entityNameLowerFirstChar}/update?id=" + dto.get${entity.weakRelationM21.sourceFieldNameUpperFirstChar}${entity.weakRelationM21.target.firstKeyField.fieldNameUpperFirstChar}();
		#else
		return "redirect:/";
		#end
	}
	
	/**
	 * @param id
	 * @return
	 */
	@PostMapping ("/delete")
	@ResponseBody
	public WoResultCode delete (String[] id) {
		${entity.entityNameLowerFirstChar}Service.delete(id);
		return WoResultCode.getSuccessCode().setMsg("删除${entity.entityCN}成功!");
	}
	
	#if(!$entity.isWeak())
	/**
	 * 加载"选择器"页面
	 * @return
	 */
	@PostMapping("/selector")
	public String loadSelector (WoSelectorParams params, Map<String, Object> map) {
		map.put("selectorParams", WoJsonUtil.toString(params));
		return "${entity.entityNameLowerFirstChar}/selector";
	}
	
	/**
	 * 加载"选择器"列表数据
	 * @param draw DataTable控件请求序列号,返回数据时需要设置该值
	 * @param start 当前页开始索引,从0开始
	 * @param length 当前页最大行数
	 * @param searchContent 查询内容
	 * @param dir 排序方式
	 * @return
	 */
	@RequestMapping("/selector/list")
	@ResponseBody
	public WoDataTable<${entity.entityName}Dto> getSelectorDataTable(Integer draw, Long start, Long length,
			@RequestParam("search[value]") String searchContent, @RequestParam("order[0][dir]") String dir) {
		WoPage<${entity.entityName}Dto> page = ${entity.entityNameLowerFirstChar}Service.getPageData(start, length, searchContent, dir);
		return new WoDataTable<${entity.entityName}Dto>(page, draw);
	}
	#end
}
